package com.gxa.saas.portal.controller.luoj;

import com.gxa.saas.core.common.JsonResult;
import com.gxa.saas.core.entity.domain.luoj.PictureDo;
import com.gxa.saas.core.entity.dto.luoj.CollectionDTO;
import com.gxa.saas.portal.service.CollectionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/commodity")
@Api(value = "收藏")
public class CollectionController {

    @Resource
    private CollectionService collectionService;

    @ApiOperation("添加到收藏")
    @PostMapping("/addCollection")
    public Map addCollection(@RequestBody CollectionDTO collection){
        Integer integer = collectionService.addCollection(collection);
        JsonResult jsonResult = new JsonResult();
        if(integer>0){
            jsonResult.setCode("200");
            jsonResult.setMsg("添加成功！");
        }else {
            jsonResult.setCode("0");
            jsonResult.setMsg("添加失败！请重新尝试");
        }
        return jsonResult.getMap();
    }

    @ApiOperation("取消到收藏")
    @PostMapping("/cancelCollection")
    public Map cancelCollection(@RequestBody CollectionDTO collection){
        Integer integer = collectionService.cancelCollection(collection);
        JsonResult jsonResult = new JsonResult();
        if(integer>0){
            jsonResult.setCode("200");
            jsonResult.setMsg("取消成功！");
        }else {
            jsonResult.setCode("0");
            jsonResult.setMsg("取消失败！请重新尝试");
        }
        return jsonResult.getMap();
    }

    @ApiOperation("查看全部收藏商品")
    @GetMapping("/queryAllCollection")
    public Map queryAllCollection(Integer userId) {
        List<PictureDo> pictureDos = collectionService.queryAllCollection(userId);
        JsonResult jsonResult = new JsonResult();

        jsonResult.setCode("200");
        jsonResult.setData(pictureDos);
        jsonResult.setMsg("查询成功！");

        return jsonResult.getMap();
    }

    @ApiOperation("清除所有收藏")
    @PostMapping("/cancelAllCollection")
    public Map cancelAllCollection(@RequestBody CollectionDTO... collections){

        for (CollectionDTO collection : collections) {
            collectionService.cancelCollection(collection);
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("全部清除成功！");
        return jsonResult.getMap();
    }
}
